package jp.ac.nitech.matlab.najm.server;

import java.net.Socket;
import java.net.ServerSocket;
import java.net.InetAddress;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.File;

public class Master {

	MetaDataMap mdm;
	
  public static void main(String[] args) {
    Master server = new Master();
    server.await();
  }

  public void await() {
	  
	  
    ServerSocket serverSocket = null;
    
    int port = Integer.parseInt(SSetting.getInstance().getSetting("master.port"));
    try {
      serverSocket =  new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
    }
    catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    }
    long time = System.currentTimeMillis();
    System.out.println("Master():: Up and Running; start"+time+" @ "+serverSocket.getInetAddress()+":"+serverSocket.getLocalPort());
    // Loop waiting for a request
    while (true) {
      Socket socket = null;
      int handles=0;
      try {
        socket = serverSocket.accept();
        System.out.println("Master():: Incoming Request @ "+(System.currentTimeMillis()-time));

        Handler service = new Handler(handles++, socket);
        
        Thread t = new Thread(service);
        t.start();
      }
      catch (Exception e) {
        e.printStackTrace();
        continue;
      }
    }
  }
}
